//
//  WDCarListView.m
//  Zhihuijinfu
//
//  Created by 王笛 on 2017/1/5.
//  Copyright © 2017年 王笛. All rights reserved.
//

#import "WDCarListView.h"
#import "WDStseries.h"

@interface WDCarListView () <UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;

@end

@implementation WDCarListView

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        
        //创建tableview
        UITableView *view = [[UITableView alloc] init];
        
        view.delegate = self;
        
        view.dataSource = self;
        
        view.separatorStyle = UITableViewCellSeparatorStyleNone;
        
        [self addSubview:view];
        
        self.tableView = view;
    }
    return self;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    self.tableView.frame = self.bounds;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.carList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"second"];
    
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"second"];
    }
    
    WDStseries *stseries = self.carList[indexPath.row];
    
    cell.textLabel.text = stseries.name;
    
    return cell;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WDScreenW, 20)];
    headView.backgroundColor = ColorForRGB(0xf0f1f2);
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(16, 0, WDScreenW, 20)];
    
    label.text = self.brand_name;
    
    label.font = [UIFont systemFontOfSize:14];
    label.textColor = ColorForRGB(0x6b6b6b);
    [headView addSubview:label];
    return headView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 20;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    WDStseries *stseries = self.carList[indexPath.row];
    
    [_delegate popViewWithSeries:stseries brandName:self.brand_name];
    
}

- (void)setDataSource:(NSArray *)dataSource
{
    _carList = dataSource;
    
    [self.tableView reloadData];
}

@end
